package com.ljh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ljh.common.JsonData;
import com.ljh.mapper.DindingMapper;
import com.ljh.mapper.OutTableMapper;
import com.ljh.pojo.Dinding;
import com.ljh.pojo.OutTable;
import com.ljh.pojo.Reservation;
import com.ljh.mapper.ReservationMapper;
import com.ljh.service.DINDINGervice;
import com.ljh.service.RESERVATIONervice;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljh.utils.ExceptionUtils;
import com.ljh.vo.DindingVo;
import com.ljh.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘剑鸿
 * @since 2021-10-25
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements RESERVATIONervice {

    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private OutTableMapper outTableMapper;
    @Autowired
    private DindingMapper dindingMapper;
    public int addRes(Reservation reservation){
        Integer ResDindingId = reservation.getDiningTableid();
        dindingMapper.updateByResdingId(ResDindingId);
        return reservationMapper.insertRes(reservation);
    }
    public JsonData selectres(ReservationVo reservationVo){
        Page<Reservation> page = PageHelper.startPage(reservationVo.getPage(), reservationVo.getLimit());
        List<Reservation> ResList = reservationMapper.selectRes(reservationVo);

        if (CollectionUtils.isEmpty(ResList)){
            ResList = Lists.newArrayList();
        }
        return JsonData.success(page.getTotal(),ResList);
    }
    public JsonData selectStop(DindingVo dindingVo){
        Page<Dinding> din = PageHelper.startPage(dindingVo.getPage(),dindingVo.getLimit());
        List<Dinding> dinList = dindingMapper.selectStopList();
        if (CollectionUtils.isEmpty(dinList)){
            dinList = Lists.newArrayList();
        }
        return JsonData.success(din.getTotal(),dinList);
    }
    public void ToOutTable(Integer id){
        System.out.println(id);
        Reservation lists = reservationMapper.selectResOne(id);
        if (lists != null){
            int ClientId =  lists.getClientid();
            int dindingId = lists.getDiningTableid();
            int People = lists.getRePeople();
            int Id = lists.getId();
            System.out.println(lists);
            OutTable outTable = new OutTable();
            outTable.setClientid(ClientId);
            outTable.setId(Id);
            outTable.setDiningTableid(dindingId);
            outTable.setPeople(People);
           int row = outTableMapper.insertOut(outTable);//新增
            if (row != 0){
                dindingMapper.updateBydingId(dindingId);
                int ResId = lists.getReservationid();
               reservationMapper.deleteById(ResId);
            }
            else{
                throw new ExceptionUtils("新增开台失败");
            }
        }
       else{
           throw new ExceptionUtils("获取预约ID失败");
        }

    }
    public void ToDelectRes(Integer id){

        reservationMapper.deleteById(id);
    }

}
